import { BasicColumn, FormSchema } from '/@/components/Table';
import { userRemarksColumn, userStatusColumn } from '@/settings/ShareTableColumns';
import { apiGetDeptsDropdown } from '@/api/http/system/senior/depts/depts.api';
import { apiGetSupplierDropdown } from '@/api/http/system/auxiliary/supplier/supplier.api';
import { userRemarkFormItem, userStatusFormItem } from '@/settings/ShareFormItem';
import { defaultRule, rules } from '@/utils/common/validator';
import { apiGetSupplierCheckNumberIsExistQuery } from '@/api/http/system/basic/supplier/supplier.api';

export const useSupplierListColumns = (): BasicColumn[] => {
  return [
    { title: '供应商名称', dataIndex: 'name' },
    { title: '供应商编号', dataIndex: 'number' },
    { title: '供应商类别', dataIndex: 'crsName' },
    { title: '应付款余额', dataIndex: 'balance' },
    { title: '所属组织', dataIndex: 'deptName' },
    { title: '所属用户', dataIndex: 'nickname' },
    userStatusColumn(),
    userRemarksColumn(),
  ];
};

export const useSupplierListSearchForm = (): FormSchema[] => {
  return [
    { field: 'name', label: '供应商名称', component: 'Input' },
    { field: 'number', label: '供应商编号', component: 'Input' },
    {
      field: 'crsId',
      label: '供应商类别',
      component: 'ApiSelect',
      componentProps: { api: apiGetSupplierDropdown },
    },
  ];
};

export const useSupplierUpdateSchemaForm = (): FormSchema[] => {
  return [
    {
      field: 'name',
      label: '供应商名称',
      component: 'Input',
      labelWidth: 100,
      rules: defaultRule(),
    },
    {
      field: 'number',
      labelWidth: 100,
      label: '供应商编号',
      component: 'Input',
      dynamicRules: ({ model, schema }) =>
        rules.uniqueFieldValidation(
          apiGetSupplierCheckNumberIsExistQuery,
          'id',
          schema,
          model,
          true,
        ),
    },
    {
      field: 'deptId',
      label: '组织结构',
      component: 'ApiTreeSelect',
      required: true,
      componentProps: { api: apiGetDeptsDropdown },
    },
    {
      field: 'userId',
      label: '所属用户',
      component: 'NodList',
      required: true,
      componentProps: ({ formModel }) => {
        return {
          scene: 'user',
          initTitle: formModel['nickname'],
        };
      },
    },
    {
      field: 'crsId',
      label: '供应商类别',
      component: 'ApiSelect',
      componentProps: { api: apiGetSupplierDropdown },
    },
    {
      field: 'rate',
      label: '增值税税率',
      labelWidth: 100,
      component: 'InputNumber',
      required: true,
      componentProps: { min: 0, max: 100, suffix: '%', precision: 2 },
    },
    { field: 'bank', label: '开户银行', component: 'Input' },
    { field: 'account', label: '银行账号', component: 'Input' },
    { field: 'tax', label: '纳税号码', component: 'Input' },
    userStatusFormItem(),
    userRemarkFormItem(),
    { field: 'more', label: '扩展信息', component: 'Input', show: false },
    { field: 'id', label: '供应商id', component: 'Input', show: false },
    { field: 'nickname', label: '供应商id', component: 'Input', show: false },
  ];
};
